//
//  UserSortPreferenceMigration.swift
//  hello
//
//  Created by 赵康 on 2025/5/15.
//

import Fluent

extension UserSortPreference {
  struct Migration: AsyncMigration {
    var name: String { "UserSortPreferenceMigration" }
    
    func prepare(on database: any Database) async throws {
      try await database.schema(UserSortPreference.schema)
        .id()
        .field(UserSortPreference.FieldKeys.user, .uuid, .references(User.schema, .id))
        .field(UserSortPreference.FieldKeys.folder, .uuid, .references(Folder.schema, .id))
        .field(UserSortPreference.FieldKeys.sortOrder, .string, .required)
        .field(UserSortPreference.FieldKeys.createdAt, .datetime, .required)
        .field(UserSortPreference.FieldKeys.updatedAt, .datetime, .required)
        .unique(on: UserSortPreference.FieldKeys.user, UserSortPreference.FieldKeys.folder)
        .create()
    }
    
    func revert(on database: any Database) async throws {
      try await database.schema(UserSortPreference.schema).delete()
    }
  }
} 